package com.qf.controller;

import cn.hutool.core.util.StrUtil;
import com.qf.entity.*;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.OrderService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.CartServiceImpl;
import com.qf.service.impl.OrderServiceImpl;
import com.qf.util.RandUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author wgy
 * @version V1.0
 * @project java2402_xiaomi
 * @package com.qf.controller
 * @company 千锋教育
 * @date 2024/6/11 11:26
 */
@WebServlet(urlPatterns = "/orderservlet")
public class OrderServlet extends BaseServlet {

    //结账预览
    public String getOrderView(HttpServletRequest request, HttpServletResponse response){
        User user= (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/login.jsp";
        }
        try {
            //创建业务对象
            CartService cartService=new CartServiceImpl();
            //1获取购物车信息
            List<Cart> cartList = cartService.findByUid(user.getId());
            if(cartList==null||cartList.size()==0){
                request.setAttribute("msg","购物车为空");
                return "/message.jsp";
            }

            //2获取收货人地址信息
            AddressService addressService=new AddressServiceImpl();
            List<Address> addList=addressService.findByUid(user.getId());

            //3放入域中
            request.setAttribute("cartList",cartList);
            request.setAttribute("addList",addList);

            return "/order.jsp";
        } catch (Exception e) {
            request.setAttribute("msg","获取订单预览失败");
            return "/message.jsp";
        }
    }

    //添加订单
    public String addOrder(HttpServletRequest request,HttpServletResponse response){
        User user= (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/login.jsp";
        }
        //获取数据
        String aid = request.getParameter("aid");
        if(StrUtil.isBlank(aid)){
            request.setAttribute("msg","提交订单失败");
            return "/message.jsp";
        }
        //1查询购物车
        try {
            CartService cartService=new CartServiceImpl();
            List<Cart> cartList = cartService.findByUid(user.getId());
            if(cartList==null||cartList.size()==0){
                request.setAttribute("msg","购物车为空");
                return "/message.jsp";
            }
            //2创建订单
            //计算总金额
            int sum=0;
            for (Cart cart : cartList) {
                sum+=cart.getMoney();
            }
            Order order=new Order(RandUtils.createOrderId(),user.getId(),sum,"1",new Date(),Integer.parseInt(aid),null);
            //创建业务对象
            OrderService orderService=new OrderServiceImpl();
            orderService.add(order,cartList);
            request.setAttribute("order",order);
            return "/orderSuccess.jsp";
        } catch (Exception e) {
            request.setAttribute("msg","订单提交失败:"+e.getMessage());
            return "/message.jsp";
        }
    }

    //订单列表
    public String getOrderList(HttpServletRequest request,HttpServletResponse response){
        User user= (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/login.jsp";
        }
        //创建业务对象
        try {
            OrderService orderService=new OrderServiceImpl();
            List<Order> orderList=orderService.findByUid(user.getId());
            //放入域中
            request.setAttribute("orderList",orderList);
            //转发
            return "/orderList.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","获取订单列表失败");
            return "/message.jsp";
        }
    }
    //查询订单详情
    public String getOrderDetail(HttpServletRequest request,HttpServletResponse response){
        User user= (User) request.getSession().getAttribute("user");
        if(user==null){
            return "redirect:/login.jsp";
        }
        String oid = request.getParameter("oid");
        if(StrUtil.isBlank(oid)){
            request.setAttribute("msg","获取订单失败");
            return "/message.jsp";
        }
        //创建业务对象
        try {
            OrderService orderService=new OrderServiceImpl();
            Order order=orderService.findByOid(oid);
            List<OrderDetail> orderDetails=orderService.findOrderDetail(oid);
            request.setAttribute("order",order);
            request.setAttribute("orderDetails",orderDetails);
            return "/orderDetail.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg","获取订单失败");
            return "/message.jsp";
        }
    }
}

